package com.myreminder.client.widgets;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DatePicker;
import com.myreminder.client.MyReminder;
import com.myreminder.data.Reminder;
import com.myreminder.data.User;
import com.myreminder.server.DAO;
import com.myreminder.service.FacebookUserService;
import com.myreminder.service.FacebookUserServiceAsync;

public class CreateReminder extends Composite {
	
	private static final FacebookUserServiceAsync facebookUserService = GWT.create(FacebookUserService.class);
	
	VerticalPanel main = new VerticalPanel();
	AbsolutePanel postit = new AbsolutePanel();
	User currentUser;
	
	TextBox inputReminderName = new TextBox();
	TextArea inputReminderDescription = new TextArea();
	DateBox inputReminderDate = new DateBox();
	
	Image buttonCreateReminder = new Image("../images/button_save.png");
	
	public CreateReminder(User u){
		this.currentUser = u;
		
		main.setWidth("100%");
		
		main.add(new Image("../images/heading_new_reminder.png"));
		
		buttonCreateReminder.setStyleName("saveReminderButton");
		
		main.add(postit);
		main.setCellHorizontalAlignment(postit, HasHorizontalAlignment.ALIGN_CENTER);
		
		postit.setPixelSize(342,229);
		postit.setStyleName("postit");
		
		postit.add(inputReminderName,45,35);
		postit.add(inputReminderDescription,45,70);
		postit.add(inputReminderDate,225,35);
		postit.add(buttonCreateReminder,290,180);
		
		inputReminderName.setStyleName("postit_input");
		inputReminderName.addStyleName("postit_input_big");
		inputReminderDescription.setStyleName("postit_input");
		inputReminderDate.setStyleName("postit_input");
		
		inputReminderDate.setWidth("50px");
		DateTimeFormat dateFormat = DateTimeFormat.getFormat("dd.MM");
		inputReminderDate.setFormat(new DateBox.DefaultFormat(dateFormat));
		inputReminderDate.setValue(new Date());
		
		inputReminderDescription.setWidth("229px");
		inputReminderDescription.setVisibleLines(7);
		
		inputReminderName.setText("title...?");
		inputReminderName.setWidth("160px");
		inputReminderName.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if(inputReminderName.getText().equals("title...?")){
					inputReminderName.setText("");
				}
			}
		});
		
		inputReminderDescription.setText("description...?");
		inputReminderDescription.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if(inputReminderDescription.getText().equals("description...?")){
					inputReminderDescription.setText("");
				}
			}
		});
		
		buttonCreateReminder.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				createReminder();
			}
		});
		
		main.add(new HTML("<div style=\"height:75px\"></div>"));
		initWidget(main);
	}
	
	private void removeErrorCodes(){
		inputReminderDate.removeStyleName("postit_input_error");
		inputReminderName.removeStyleName("postit_input_error");
	}
	
	private void createReminder(){
		removeErrorCodes();
		boolean error = false;
		if(inputReminderDate.getValue() == null){
			inputReminderDate.addStyleName("postit_input_error");
			error = true;
		}
		if(inputReminderName.getText().equals("")){
			inputReminderName.addStyleName("postit_input_error");
			error = true;
		}
		if(error) return;
		facebookUserService.createReminder(currentUser, inputReminderName.getValue(), inputReminderDate.getValue(), inputReminderDescription.getText(), new AsyncCallback<Reminder>() {
			@Override
			public void onSuccess(Reminder result) {
				currentUser.addReminder(result);
				MyReminder.table.updateTable();
				// remove Error codes!
				removeErrorCodes();
			}
			@Override
			public void onFailure(Throwable caught) {
			}
		});
	}
}
